<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 整合 Caffeine 缓存：高性能本地缓存实战指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-language);
            position: absolute;
            top: 0.5rem;
            right: 0.5rem;
            font-size: 0.75rem;
            color: #888;
            text-transform: uppercase;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .annotation-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2rem;
            border-radius: 16px;
            margin: 2rem 0;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                Spring Boot 整合 Caffeine 缓存
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                高性能本地缓存实战指南
            </p>
            <div class="flex justify-center space-x-6 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    <span>极速性能</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-memory mr-2"></i>
                    <span>智能内存管理</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    <span>简单集成</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>现代的微服务架构中，缓存已经成为提升系统性能、降低数据库压力和提高响应速度的关键技术之一。对于Java开发者而言，Spring Boot作为一种开发框架，不仅提供了灵活的缓存机制，而且通过与一些缓存库（如Caffeine）结合，能够进一步优化应用性能。
            </p>
        </div>

        <!-- Architecture Diagram -->
        <div class="mermaid">
            <script type="text/mermaid">
                graph TB
                    A[客户端请求] --> B{缓存检查}
                    B -->|缓存命中| C[返回缓存数据]
                    B -->|缓存未命中| D[查询数据库]
                    D --> E[存储到缓存]
                    E --> F[返回数据]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4CAF50,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#FF9800,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#2196F3,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#4CAF50,stroke:#fff,stroke-width:2px,color:#fff
            </script>
        </div>

        <!-- What is Caffeine -->
        <section class="mt-16">
            <h2 class="text-4xl font-bold mb-8 text-gradient">1. 什么是Caffeine缓存？</h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <p class="text-lg mb-6 text-gray-700">Caffeine是一个高性能的Java缓存库，它的主要特点包括：</p>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="text-center card-hover">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-tachometer-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">高性能</h3>
                        <p class="text-gray-600">Caffeine具有极快的读写性能，适合高并发的场景。</p>
                    </div>
                    <div class="text-center card-hover">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-cogs"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">丰富的缓存策略</h3>
                        <p class="text-gray-600">支持基于时间、大小等多种缓存失效策略。</p>
                    </div>
                    <div class="text-center card-hover">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-recycle"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">自动回收</h3>
                        <p class="text-gray-600">内存占用达到限制后会自动回收不常用的缓存。</p>
                    </div>
                </div>
                
                <p class="text-lg mt-6 text-gray-700">
                    相较于其他缓存解决方案（如Guava），Caffeine提供了更高的性能和更多的缓存控制选项，适合在Spring Boot中作为本地缓存解决方案。
                </p>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Spring Cache Integration -->
        <section>
            <h2 class="text-4xl font-bold mb-8 text-gradient">2. Spring Cache与Caffeine集成</h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <p class="text-lg mb-6 text-gray-700 indent-8">
                    Spring Cache是Spring Framework提供的一种缓存抽象，简化了缓存操作。你可以通过<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Cacheable</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@CachePut</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@CacheEvict</code>等注解轻松地对方法进行缓存操作，而不需要手动管理缓存的存取。
                </p>

                <h3 class="text-2xl font-bold mb-4 text-gray-800">2.1 Caffeine缓存集成的优势</h3>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">性能提升：</strong>
                            <span class="text-gray-600">Caffeine缓存对常用数据的快速访问能够大幅提高应用性能，减少数据库负载。</span>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">内存管理：</strong>
                            <span class="text-gray-600">Caffeine通过LRU（最近最少使用）策略、最大缓存大小限制、缓存过期等机制自动管理内存，避免内存泄漏。</span>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">易于集成：</strong>
                            <span class="text-gray-600">Spring Cache抽象使得与Caffeine的集成非常简单，开发者无需管理缓存的底层实现。</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Integration Steps -->
        <section>
            <h2 class="text-4xl font-bold mb-8 text-gradient">3. 集成步骤</h2>
            
            <!-- Step 1 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                <h3 class="text-2xl font-bold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">1</span>
                    添加依赖
                </h3>
                <p class="text-gray-700 mb-4 indent-8">
                    我们需要在Spring Boot项目的<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">pom.xml</code>文件中添加Caffeine和Spring Cache相关依赖：
                </p>
                <div class="code-block" data-language="xml">
                    <pre>&lt;dependencies&gt;
    &lt;!-- Spring Boot Cache依赖 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;!-- Caffeine缓存依赖 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.github.ben-manes&lt;/groupId&gt;
        &lt;artifactId&gt;caffeine&lt;/artifactId&gt;
        &lt;version&gt;2.9.2&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</pre>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                <h3 class="text-2xl font-bold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">2</span>
                    配置Caffeine缓存
                </h3>
                <p class="text-gray-700 mb-4 indent-8">
                    在<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">application.properties</code>或<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">application.yml</code>文件中配置Caffeine缓存。比如设置最大缓存大小和过期策略：
                </p>
                <div class="code-block" data-language="properties">
                    <pre># 配置Caffeine缓存